package com.example.navigation.controller;

import com.example.navigation.dto.NavigationResponse;
import com.example.navigation.dto.PlaceSearchResponse;
import com.example.navigation.service.BaiduMapsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/navigation")
public class NavigationController {

    private final BaiduMapsService baiduMapsService;

    @Autowired
    public NavigationController(BaiduMapsService baiduMapsService) {
        this.baiduMapsService = baiduMapsService;
    }

    /**
     * 获取驾车导航路线
     * @param origin 起点坐标，格式为"纬度,经度"，例如"39.915,116.404"
     * @param destination 终点坐标，格式为"纬度,经度"
     * @param alternatives 是否返回备选路线，默认为true
     * @return 导航响应对象
     */
    @GetMapping("/driving")
    public ResponseEntity<NavigationResponse> getDrivingDirections(
            @RequestParam String origin,
            @RequestParam String destination,
            @RequestParam(defaultValue = "true") boolean alternatives) {
        
        NavigationResponse response = baiduMapsService.getDrivingDirections(origin, destination, alternatives);
        
        // 如果API没有返回路线标签或需要自定义标签，可以在这里设置
        if (response.getStatus() == 0 && response.getResult() != null && response.getResult().getRoutes() != null) {
            int routeCount = response.getResult().getRoutes().size();
            
            // 只有在路线没有标签时才设置默认标签
            for (int i = 0; i < routeCount; i++) {
                NavigationResponse.Route route = response.getResult().getRoutes().get(i);
                if (route.getTag() == null || route.getTag().isEmpty()) {
                    if (i == 0) {
                        route.setTag("推荐路线");
                    } else if (i == 1) {
                        route.setTag("备选路线1");
                    } else if (i == 2) {
                        route.setTag("备选路线2");
                    } else {
                        route.setTag("备选路线" + i);
                    }
                }
            }
        }
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 搜索地点
     * @param keyword 搜索关键词
     * @param region 搜索区域，默认为"全国"
     * @return 地点搜索响应对象
     */
    @GetMapping("/place/search")
    public ResponseEntity<PlaceSearchResponse> searchPlace(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "全国") String region) {
        
        PlaceSearchResponse response = baiduMapsService.searchPlace(keyword, region);
        return ResponseEntity.ok(response);
    }
} 